package pri.zxx.testdemo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class PCAction {

    public static void load() throws IOException {
        String strurl = "https://baijiahao.baidu.com/s?id=1589546094825996322&wfr=spider&for=pc";
        ByteBuffer cons = ByteBuffer.allocate(1024);
        try {
            URL url = new URL(strurl);
            URLConnection conn = url.openConnection();
            InputStream is = conn.getInputStream();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int index;
            while ((index = is.read()) != -1) {
                bos.write(index);
            }
            cons.put(bos.toByteArray());
            bos.close();
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        cons.flip();
        Path path = Paths.get("D:/", "ceshi.html");
        SeekableByteChannel seekableByteChannel = Files.newByteChannel(path, StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        seekableByteChannel.write(cons);
        seekableByteChannel.close();
        //String content = new String(cons, "UTF-8");
        // System.out.println(content);
    }

    public static void main(String[] args) throws IOException {
        PCAction.load();
    }
}
